package com.kinon.norogamilk.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.kinon.norogamilk.R;
import com.kinon.norogamilk.http.MilkApi;
import com.kinon.norogamilk.http.StrCallback;
import com.kinon.norogamilk.ui.base.BaseActivity;
import com.kinon.norogamilk.util.QRCodeUtil;

import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by lhqq on 2017-12-18.
 * 会员取货
 */

public class MemberTakeActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout ll_memberCode;
    private LinearLayout ll_memberTake;
    private Button btn_back;
    private Button btn_normal;
    private Button btn_heat;
    private Button btn_take;
    private ImageView img_mumberCode;

    private boolean isHeat = false;
    private boolean isPauseFind = false;
    private String strData = "";
    private Handler myHandler = new Handler();
    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            if (!isPauseFind) {
                machineWs();

                myHandler.postDelayed(mRunnable, 1000);
//
            }

        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_membertake_layout);
        initView();
    }

    private void initView() {
        btn_back = findViewById(R.id.btn_back);
        btn_normal = findViewById(R.id.btn_normal);
        btn_heat = findViewById(R.id.btn_heat);
        btn_take = findViewById(R.id.btn_take);
        ll_memberCode = findViewById(R.id.ll_memberCode);
        ll_memberTake = findViewById(R.id.ll_memberTake);
        img_mumberCode = findViewById(R.id.img_mumberCode);
        getMemberCode();


        addOncilck();
    }

    private void addOncilck() {
        btn_back.setOnClickListener(this);
        btn_normal.setOnClickListener(this);
        btn_heat.setOnClickListener(this);
        btn_take.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_normal:
                isHeat = false;
                break;
            case R.id.btn_heat:
                isHeat = true;
                break;
            case R.id.btn_take:
//                getProductByCode("0001");

                Intent intent = new Intent(MemberTakeActivity.this,
                        PaySuccessActivity.class);
                intent.putExtra("isHeat", isHeat);
                intent.putExtra("strTakeData", strData);
                startActivity(intent);
                finish();

                break;
            default:
                break;
        }
    }

    //获取二维码
    private void getMemberCode() {
        Call<String> call = MilkApi.getInstance().getURL(sn);
        call.enqueue(new StrCallback<String>() {
            @Override
            public void onSuccess(Response<String> response, String data) {
                myHandler.post(mRunnable);
//                img_mumberCode.setImageBitmap(QRCodeUtil.createQRCode(200,200,data));
                img_mumberCode.setImageBitmap(QRCodeUtil.createQRImage(data, 180, 180, null));
            }

            @Override
            public void onError(int code, String error) {
                mToast(error);
            }
        });
    }

    //是否出货
    private void machineWs() {
        Call<String> call = MilkApi.getInstance().machineWs(sn);
        call.enqueue(new StrCallback<String>() {
            @Override
            public void onSuccess(Response<String> response, String data) {
                isPauseFind = true;
                strData = data;
                visibility();

//                mToast("出货成功");
//                Intent intent=new Intent(MemberTakeActivity.this,PaySuccessActivity.class);
//                startActivity(intent);
//                finish();
            }

            @Override
            public void onError(int code, String error) {
//                mToast(error);
            }
        });
    }

    //是否出货
    private void getProductByCode(String sn) {
        Call<String> call = MilkApi.getInstance().getProductByCode(sn);
        call.enqueue(new StrCallback<String>() {
            @Override
            public void onSuccess(Response<String> response, String data) {
                isPauseFind = true;
                visibility();

//                mToast("出货成功");
//                Intent intent=new Intent(MemberTakeActivity.this,PaySuccessActivity.class);
//                startActivity(intent);
//                finish();
            }

            @Override
            public void onError(int code, String error) {
//                mToast(error);
            }
        });
    }

    private void visibility() {
        ll_memberCode.setVisibility(View.GONE);
        ll_memberTake.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onPause() {
        super.onPause();
        isPauseFind = true;
        myHandler.removeCallbacks(mRunnable);
    }
}
